// 导入express模块
var express = require('express');
// 导入路由模块
var router = express.Router();
// 导入url模块
var url = require('url');
// 导入mysql模块 
var mysql = require('mysql');
// 导入数据库配置信息
var dbconfig = require('../db/DBConfig');
// 导入SQL查询语句
var user = require('../sql/Usersql');
// 导入自定义公共类
var util = require('../utils/util');
// 使用DBConfig.js的配置信息创建一个MySQL连接池
var pool = mysql.createPool(dbconfig.mysql);

/* POST users listing. */
/* 登录接口 */
router.post('/login', function(req, res, next) {
    // 获取请求字段
    let params = {
        user: req.body.user,
        pwd: req.body.pwd
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        // 查询用户名是否存在
        connection.query(user.queryUserName(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                // 查询用户名密码是否正确
                connection.query(user.queryUNP(params), function(err, result) {
                    if (util.isEmpty(result)) {
                        res.send({ "success": false, "data": {}, "msg": "用户名或密码错误", "code":400 });
                    } else {
                        if (result.length == 1) {
                            var obj = params.user;
                            res.send({ "success": true, "data": {result}, "msg": "登录成功", "code":200 });
                            req.session.user = params.user;
                        } else {
                            res.send({ "success": false, "data": {}, "msg": "用户名或密码错误", "code":400 });
                        }
                    }
                });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "用户名不存在", "code":500 });
            }
        });
    })
});
/* 注册用户 */
router.post('/registered', function(req, res, next) {
    // 获取请求字段
    let params = {
        colleage : req.body.col,
        xnum : req.body.xn,
        user: req.body.user,
        pwd: req.body.pwd,
        type: req.body.type,
        headimg: req.body.headimg || '',
        addtime: util.CurentDay(),
        edittime: util.CurentDay()
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        // 查询用户名是否存在
        connection.query(user.queryUserName(params), function(err, results) {
            if (util.isEmpty(results)) {
                // 插入用户名密码
                connection.query(user.insertData(params), function(err, result) {
                    if (!util.isEmpty(result)) {
                        res.send({ "success": true, "data": {}, "msg": "注册成功", "code":200 });
                    } else {
                        res.send({ "success": false, "data": {}, "msg": "参数输入不合法", "code":400 });
                    }
                });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "用户名已经存在", "code":400 });
            }
        });
    });
});
// 获取用户
router.post('/get', function(req, res, next) {
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(user.queryAllUser(), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "获取失败", "code":400 });
            }
        });
    })
});
// 获取学生
router.post('/getstu', function(req, res, next) {
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(user.queryAllStu(), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "获取失败", "code":400 });
            }
        });
    })
});
// 删除用户
router.post('/del', function(req, res, next) {
    let params = {
        ID: req.body.ID
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(user.delUser(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "删除成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "删除失败", "code":400 });
            }
        });
    })
});
// 更新用户
router.post('/update', function(req, res, next) {
    let params = {
        ID: req.body.id,
        name: req.body.name,
        pwd: req.body.pwd,
        edittiem: util.CurentTime(),
        colleage:req.body.colleage,
        xum : req.body.xnum
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(user.updateUser(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "更新成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "更新失败", "code":400 });
            }
        });
    })
});
// 更新密码
router.post('/changepass', function(req, res, next) {
    let params = {
        ID: req.body.ID,
        pwd: req.body.pwd,
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(user.changePass(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "更新成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "更新失败", "code":400 });
            }
        });
    })
});
// 通过ID获取
router.post('/getbyid', function(req, res, next) {
    let params = {
        ID: req.body.ID
    };
    // 启用连接池查询
    pool.getConnection(function(err, connection) {
        connection.query(user.queryUserByID(params), function(err, results) {
            // 查询结果
            if (!util.isEmpty(results)) {
                res.send({ "success": true, "data": {results}, "msg": "获取成功", "code":200 });
                connection.release();
            } else {
                res.send({ "success": false, "data": {}, "msg": "获取失败", "code":400 });
            }
        });
    })
});
module.exports = router;